/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.config;

import java.util.Observable;

import org.jconfig.ConfigurationManager;
import org.jconfig.ConfigurationManagerException;
import org.jconfig.event.FileListener;
import org.jconfig.event.FileListenerEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/** The Listener that reloads config everytime the config file is changed
 *
 * @author GiangLT
 */
public class ConfigFileListener extends Observable implements FileListener {

    private static final Logger logger = LoggerFactory.getLogger(ConfigFileListener.class);
    private static ConfigFileListener instance;
    private String configName;

    public synchronized static ConfigFileListener getInstance(String configName) {
        if (instance == null) {
            instance = new ConfigFileListener(configName);
        }
        return instance;
    }
    
    private ConfigFileListener(String configName){
        this.configName = configName;
    }

    /**
     *
     * @param arg0
     */
    @Override
    public void fileChanged(FileListenerEvent arg0) {
        try {
            ConfigurationManager.getInstance().reload(configName);
            logger.info("Config file changed. System config reloaded.");
            
            setChanged();
            notifyObservers();
        } catch (ConfigurationManagerException ex) {
            logger.error("", ex);
        }
    }
}
